
public class LargeKey {

	char[] Plain2Cipher;
	char[] Cipher2Plain;
	int[] Bitmap;
	
	public LargeKey()
	{
		Plain2Cipher = new char[52];
		Cipher2Plain = new char[52];
		Bitmap = new int[52];
		
		for(int i = 0; i < 52; i++){
			Bitmap[i] = 0;
			Plain2Cipher[i] = 0;
			Cipher2Plain[i] = 0;
		}
	}
	
	public LargeKey(LargeKey other)
	{
		Plain2Cipher = new char[52];
		Cipher2Plain = new char[52];
		Bitmap = new int[52];
		
		for(int i = 0; i < 52; i++){
			Bitmap[i] = other.Bitmap[i];
			Plain2Cipher[i] = other.Plain2Cipher[i];
			Cipher2Plain[i] = other.Cipher2Plain[i];
		}
	}
	
	public boolean isFullyMapped(){
		for (int i=0; i < 52; i++)
			if (Bitmap[i]==0)
				return false;
		
		return true;
	}
	
	public boolean Mapped(char index)
	{
		int bit;
		
		if(index >= 'a' && index <= 'z')
			bit = Bitmap[index - 97];
		else
			bit = Bitmap[index - 39];
		
		if(bit == 0)
			return false;
		else
			return true;
	}
	
	public boolean PlainMapped(char index)
	{
		if(index >= 'a' && index <= 'z')
			return Plain2Cipher[index - 97]!=0;
		else
			return Plain2Cipher[index - 39]!=0;
	}
	
	public void SetValue(char index, char ch, String str)
	{
		if(str.compareTo("cipher") == 0)
		{
			if(index >= 'a' && index <= 'z')
			{
				Cipher2Plain[index - 97] = ch;
				Bitmap[index - 97] = 1;
			}
			else
			{
				Cipher2Plain[index - 39] = ch;
				Bitmap[index - 39] = 1;
			}
		}
		else
			if(index >= 'a' && index <= 'z')
				Plain2Cipher[index - 97] = ch;
			else
				Plain2Cipher[index - 39] = ch;
	}
	
	public char GetValue(char index, String str)
	{
		char res;
		
		if(str.compareTo("cipher") == 0)
		{
			if(index >= 'a' && index <= 'z')
				res = Cipher2Plain[index - 97];
			else
				res = Cipher2Plain[index - 39];
		}
		else
			if(index >= 'a' && index <= 'z')
				res = Plain2Cipher[index - 97];
			else
				res = Plain2Cipher[index - 39];
		
		return res;
	}
	
	public String LargeKeyToString()
	{
		int i; 
		StringBuilder sb = new StringBuilder();
		
		for(i = 0; i < 52; i++)
			if(Plain2Cipher[i] != 0)
			{
				if(i < 26)
					sb.append((char)(i + 97));
				else
					sb.append((char)(i + 39));
				sb.append(' ');
				sb.append(Plain2Cipher[i]);
				sb.append(System.getProperty("line.separator"));
			}
		
		return sb.toString();
	}
	
	public String LargeKeyReversedToString()
	{
		int i; 
		StringBuilder sb = new StringBuilder();
		
		for(i = 0; i < 52; i++)
			if(Bitmap[i] == 1)
			{
				if(i < 26)
					sb.append((char)(i + 97));
				else
					sb.append((char)(i + 39));
				sb.append(' ');
				sb.append(Cipher2Plain[i]);								
				sb.append(System.getProperty("line.separator"));
			}
		
		return sb.toString();
	}
	
	public int CountMissingValues(){
		int i, count=0;
		for (i=0; i<Plain2Cipher.length; i++){
			if (Plain2Cipher[i]==0)
				count++;
		}
		
		return count;
	}
	
	public char TranslateIndexToChar(int index){
		if(index < 26)
			return((char)(index + 97));
		else
			return((char)(index + 39));
	}
	
	public String toString(){
		return LargeKeyToString();
	}
}





